var salt = sessionStorage.getItem('usersalt');
var courseId = getQueryVariable("id");

layui.use(['form', 'layer', 'laytpl', 'carousel'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laytpl = layui.laytpl,
		carousel = layui.carousel;

	/*验证登录*/
	if(!sessionStorage.getItem('username')) {
		layer.msg('请先登录');
		setTimeout("top.location.href='../../../index.html'", 1000);
	}

    $.post(url + "/baseUsers/getInfo",{Token:salt},function (data) {
        var data = data.data;
        $("#userName").html(data.username);
    })
	
	//下载
	$(".download_btn").click(function(){
		window.open(url + '/attachment/seekFile/' + course.attId);
	});
	
	//全屏
	$(".allscreen_btn").click(function(){
        var ele = document.documentElement;
        if (ele .requestFullscreen) {
            ele .requestFullscreen();
        } else if (ele .mozRequestFullScreen) {
            ele .mozRequestFullScreen();
        } else if (ele .webkitRequestFullScreen) {
            ele .webkitRequestFullScreen();
        }
	});
	

	
});

function studyTime() {
	$.post(url+"/course/studyTime",{Token: salt, id : courseId},function (data) {
    })
}

function setTableHeight(){
    var bodyHeight = $("body").height();
    var tabHeight = $(".topsty").height();
    var firLineHeight = $("#firLine").height();
    $("#secLine").height(bodyHeight - tabHeight - firLineHeight - 40);
}
setTableHeight();

var carouselIndex;
function getCourseInfo(){
    $.ajax({
        url: url + "course/getInfo",
        dataType: "json",
        async: true,
        type: "post",
        data: {
            Token: salt,
            id : courseId
        },
        success: function(data){
            if (data.code == 0) {
                course = data.data;
                $("#courseName").html(course.name);


                var record = {url : "",videoId : []};
                record.url = url;
                record.videoId = course.attId;
                //渲染课程列表html
                var videoHtml = $("#videoHtml").html();
                layui.use(['laytpl'], function() {
                	var laytpl = layui.laytpl;
                    laytpl(videoHtml).render(record, function(html){
                        $("#videoViewer").html(html);
                    });
				})

                $("#video").attr("src",url + "file/" + course.videoUrl);
                studyTime();
            } else {
                setTimeout(function(){
                    layer.msg(data.msg);
                }, 1000);
            }
        },
        error: function(error){
        }
    });
}

var course;

if (courseId != false) {
    getCourseInfo();
} else {
    layer.msg("参数id不存在");
    setTimeout(function(){
        history.go(-1);
    }, 500);
}

/*------------------页面计时.start------------------*/
var monitor_plus = {
    click_time : new Date(),
    leave : 180,//3分钟页面未操作时间
    timer : true,//是否学习计时
    pageTime : 300,//本页3分钟学习时间
    studyTime : 0,//一共学习时间
    init : function(){
    	clearTimeout(timeOut)
        this.timer = true;
        this.pageTime = 300;
        $("#remainTime").removeClass("layui-hide").addClass("layui-show");
        $("#continueTime").removeClass("layui-show").addClass("layui-hide");
        this.countimer();
    },
    countimer : function() {
        var now = new Date();
        var span = parseInt(now - this.click_time) / 1000;
        if (span > this.leave) {
            //3分钟无操作弹出继续计时按钮
            this.timer = false;
            var layerIndex = layer.confirm("继续学习", {
                icon: 3,
                title: false,
                closeBtn: 0,
                btn: ["确定"]
            }, function(){
                monitor_plus.timer = true;
                layer.close(layerIndex);
                monitor_plus.click_time = new Date();
                monitor_plus.countimer();
            });
        } else {
            if (this.pageTime == 0) {
                this.timer = false;
                $("#remainTime").removeClass("layui-show").addClass("layui-hide");
                $("#continueTime").removeClass("layui-hide").addClass("layui-show");
                $(".tddlabbg2").css("width", "100%");
                $(".tddlabbg3").css("width", "0%");
            }

            timeOut = setTimeout(function(){
                monitor_plus.countimer();
            }, 1000);
        }
        if (this.timer == true) {
            this.pageTime--;
            this.studyTime++;

            //学习时间显示
            var html = changeSecondShow(this.studyTime);
            $("#studyTime").html(html);

            //本页剩余时间显示
            html = changeSecondShow(this.pageTime);
            $("#pageRemainTime").html(html);

            //进度条显示
            var percent2 = numMulti(numDiv(this.pageTime, 300), 100);
            var percent3 = numSub(100, percent2);
            $(".tddlabbg2").css("width", percent2 + "%");
            $(".tddlabbg3").css("width", percent3 + "%");
        }
    },
    update : function() {
        this.click_time = new Date();
    }
};

$(document).ready(function() {
	/* 鼠标移动事件 */
    $(document).mouseover(function(){
        monitor_plus.update(); //更新操作时间
    });
    monitor_plus.countimer(); //更新操作时间
});

/*------------------页面计时.end------------------*/

//秒数转换成时分秒
function changeSecondShow(secondTotal){
    var hour = parseInt(numDiv(secondTotal, 3600));
    var minute = parseInt(numDiv(secondTotal, 60));
    var second = secondTotal % 60;
    var html = "";
    if (hour > 0) {
        html = hour + "h";
    }
    if (minute > 0) {
        html += minute + "min";
    }
    html += second + "s";
    return html;
}